﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using Xyz.DataObjects;
using Xyz.DataObjects.Interfaces;
using Xyz.BusinessObjects;
using Xyz.Service.DataTransferObjects;
using Xyz.Service.ServiceContracts;

namespace Xyz.Service
{
    /// <summary>
    //// This service will handle all methods for Flight
    /// </summary>
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    public  class FlightService : IFlightService
    {
        public static readonly IFlightDao flightDao = DataAccess.FlightDao;

        public List<AirportDTO> GetAirports()
        {
            return Mapper.ToDto(flightDao.GetAirports()); 
        }

        public FlightDTO GetFlightStatus(String flightCode, DateTime FlightDate, String FlightAirport, bool IsArrival)
        {
            Flight flight = flightDao.GetFlightStatus(flightCode, FlightDate, FlightAirport, IsArrival);
            if (flight != null)
                return Mapper.ToDto(flight);
            else
                return null;
        }

        public AircraftDTO getAircraftByFlightId(int flightId)
        {
            Aircraft aircraft = flightDao.GetAircraftByFlightId(flightId);
            
            return Mapper.ToDto(aircraft);
          
        }

        public void InsertFlight(FlightDTO flight)
        {
            var flg = Mapper.FromDto(flight);

            if (flg.Validate())
            {
                flightDao.InsertFlight(flg);
            }
        }

        public List<AircraftDTO> GetAircrafts()
        {
            return Mapper.ToDto(flightDao.GetAircrafts());
        }

        public List<TerminalDTO> GetTerminalByAirportId(string airportCode)
        {
            return Mapper.ToDto(flightDao.GetTerminalByAirportId(airportCode));
        }

        public List<FlightDTO> GetFlights()
        {
            return Mapper.ToDto(flightDao.GetFlights());
        }
        public FlightDTO GetFlightByFlightId(int flightId)
        {
            Flight flight = flightDao.GetFlightByFlightId(flightId);
            if (flight != null)
                return Mapper.ToDto(flight);

            return null;
        }

        public void UpdateFlight(FlightDTO flight)
        {
            var flg = Mapper.FromDto(flight);

            if (flg.Validate())
            {
                flightDao.UpdateFlight(flg);
            }
        }

        public void DeleteFlight(int flightId)
        {
            flightDao.DeleteFlight(flightId);
        }

        public List<FlightDTO> GetFlightsByFlightCode(string flightCode)
        {
            List<Flight> flights = flightDao.GetFlightsByFlightCode(flightCode);
            if (flights != null)
            {
                return Mapper.ToDto(flights);
            }

            return null;
        }

        public List<FlightDTO> GetFlights(DateTime departureDate, String departureAirport, String arrivalAirport)
        {
            return Mapper.ToDto(flightDao.GetFlights(departureDate, departureAirport, arrivalAirport));
        }
    }
}
